const baseURL = 'https://qymmc.cn';

function request(options = {}) {
  const url = options.url.startsWith('http') ? options.url : `${baseURL}${options.url}`;

  // 打印完整请求信息
  console.log('请求配置:', {
    url,
    method: options.method,
    data: options.data,
    header: options.header
  });

  return new Promise((resolve, reject) => {
    const requestTask = wx.request({
      ...options,
      url,
      timeout: 30000,  // 增加超时时间
      enableHttp2: false,  // 禁用 HTTP2
      enableQuic: false,   // 禁用 QUIC
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token') || '',
        ...options.header
      },
      success: (res) => {
        console.log('请求成功:', res);
        console.log(res.statusCode)
        resolve(res.data);
      },
      fail: (err) => {
  
        console.error('请求失败:', err);
        reject(err);
      }
    });

    // 监听请求状态
    requestTask.onHeadersReceived((res) => {
      console.log('收到响应头:', res);
    });
  });
}

module.exports = request;